package com.along.game.service.impl;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import org.springframework.stereotype.Service;

import com.along.framework.utils.FileUpVO;
import com.along.framework.web.BaseService;
import com.along.game.service.CommonUtilService;


@Service("commonUtilService")
public class CommonUtilServiceImpl   extends BaseService implements CommonUtilService {

	
	
	   /**
	 * 文本编辑器中图片上传
	 * @return String fileName
	 * @throws BusException
	 * @throws  
	 */
	public String fileUpLoad(FileUpVO fileUpVO)   {
		String newFileName="";
		
		if(!fileUpVO.getUploadFileName().equals("") || fileUpVO.getUploadFileName() == null){
			String old_fileName = fileUpVO.getUploadFileName();
			String[] fileNames = old_fileName.split("\\.");
			String fileName="";
			//获得文件后缀
			for (int i = 0; i < fileNames.length; i++) {
				fileName = fileNames[i];
			}
			//获得日期
			Calendar c = Calendar.getInstance();
			Date date = c.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String startTime = sdf.format(date);
			//获得四位随机数
			Random random = new Random();
			String rdStr="";
			do{
				int randomInt = Math.abs(random.nextInt())%10+48; 
				
				char rdChar = (char) randomInt;
			
				rdStr += Character.toString(rdChar);
				
			}while(rdStr.length()<4);
			
			
			String [] a = startTime.split(" ");
			startTime="";
			for (int i = 0; i < a.length; i++){
				if(i!=a.length-1)
					startTime += a[i]+"_";
				else
					startTime += a[i];
			}
			
			String[] b = startTime.split(":");
			startTime="";
			for (int i = 0; i < b.length; i++){
				if(i!=b.length-1)
					startTime += b[i]+"-";
				else
					startTime += b[i];
			}
			
			
			newFileName = startTime+"_"+rdStr+"."+fileName;
			fileUpVO.setUploadFileName(newFileName);
			try {
				upFile(fileUpVO, "");
			} catch (IOException e) {
				e.printStackTrace();
				
			}
		}
		
		return newFileName;
	}
	
	
	
	private boolean upFile(FileUpVO fileUpVO,String filePathStr) throws IOException{
		
		String strPath = null;
		
		if(filePathStr.equals("") || filePathStr == null)
			strPath = fileUpVO.getFilePath();
			
		System.out.println(fileUpVO.getRootPath());
		//FileInputStream input = new FileInputStream(fileUpVO.getUploadFileName()); 
		FileInputStream input=null;
		
		if(fileUpVO.getRootPath()==null)
		 input = new FileInputStream(fileUpVO.getUpload()); 
		else
			input = new FileInputStream(fileUpVO.getUpload()); 
		//input = new FileInputStream(fileUpVO.getRootPath()); 
		
		FileOutputStream fos = new FileOutputStream(strPath+fileUpVO.getUploadFileName());
	
		byte[] buffer = new byte[1024];
		int len = 0;
		int offset=0;
		
		while((len = input.read(buffer))>0)
				fos.write(buffer);
	
	
		fos.close();
		input.close();
		return true;
	}
    
	
}
